<template>
  <div class="app-container">
    <div class="filter-container">


      <el-input  style="width: 200px;"   v-model="query.user_name" placeholder="姓名" class="filter-item"  ></el-input>
      <el-input  style="width: 200px;"   v-model="query.phone" placeholder="手机号" class="filter-item"  ></el-input>

      <!--      <span>外租单位：</span>-->
      <el-select filterable v-model="query.rent_unit_id" placeholder="外租单位" class="filter-item" >
        <el-option :label="'全部'" :value="0" ></el-option>
        <el-option v-for="unit in unitList" :label="unit.unit_name" :value="unit.id" ></el-option>
      </el-select>

<!--      <span>车牌号：</span>-->
      <el-select filterable v-model="query.car_no" placeholder="车牌号" class="filter-item" >
        <el-option :label="'全部'" :value="0" ></el-option>
        <el-option v-for="car in allCarLst" :label="car.car_no" :value="car.car_no" ></el-option>
      </el-select>


      <el-button style="margin-left: 10px" @click="queryRecords" type="primary" class="filter-item">查询</el-button>
      <el-button @click="reload" type="danger" class="filter-item">刷新</el-button>
<!--      <el-button @click="openAddDialog" type="primary" class="filter-item">+派车单</el-button>-->
<!--      <el-button @click="handleDelete(undefined,undefined)" type="primary" class="filter-item">批量取消</el-button>-->
<!--      <el-button @click="handleStatus(undefined,undefined,2)" type="primary" class="filter-item">批量下架</el-button>-->
<!--      <el-button @click="handleTop(undefined,undefined,1)" type="primary" class="filter-item">批量置顶</el-button>-->
    </div>

      <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
      v-horizontal-scroll="'always'"
      @selection-change="handleSelectionChange"
    >
<!--    <el-table-column type="selection" min-width="55" />-->
    <el-table-column prop="id" label="ID" min-width="80" />
    <el-table-column prop="car_no" label="车牌号" min-width="150" />
    <el-table-column prop="status_name" label="状态" min-width="150" />
    <el-table-column prop="fix_factory" label="维修车厂" min-width="150" />
    <el-table-column prop="fix_people" label="维修人员" min-width="150" />
    <el-table-column prop="question_desc" label="问题描述" min-width="200" />
    <el-table-column prop="get_user.name" label="用户名" min-width="80" />
    <el-table-column prop="get_user.phone" label="用户手机" min-width="150" />
    <el-table-column prop="rent_unit_name" label="所属单位" min-width="150" />
    <el-table-column prop="rent_unit_name" label="图片" min-width="100" >
      <template slot-scope="scope">
        <el-image v-if="scope.row.imgs.length>0"
          style="width: 50px; height: 50px"
          :src="scope.row.first_img"
          :preview-src-list="scope.row.imgs">
        </el-image>
      </template>
    </el-table-column>
    <el-table-column prop="rent_unit_name" label="图片" min-width="100" >
      <template slot-scope="scope">
        <el-image v-if="scope.row.fix_imgs.length>0"
          style="width: 50px; height: 50px"
          :src="scope.row.first_fix_img"
          :preview-src-list="scope.row.fix_imgs">
        </el-image>
      </template>
    </el-table-column>

    <el-table-column prop="beizhu" label="备注" min-width="150" />
    <el-table-column prop="created_at" label="创建时间" min-width="160" />
    <el-table-column fixed="right" label="操作" min-width="130">
      <template slot-scope="scope">
<!--        <el-button v-show="scope.row.status==3" size="mini" type="text" @click="handleBackCar(scope.$index, scope.row)">收车</el-button>-->
<!--        <el-button size="mini" type="text" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>-->
<!--        <el-button size="mini" type="text" @click="handlePic(scope.$index, scope.row)">图片</el-button>-->
        <el-button size="mini" type="text" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        <el-button size="mini" type="text" @click="showCheck(scope.$index, scope.row)">审核</el-button>
        <el-button size="mini" type="text" @click="showEnd(scope.$index, scope.row)">完成</el-button>
      </template>
    </el-table-column>
    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pagination.page"
      :page-sizes="[10, 20, 100, 200]"
      :page-size="pagination.pageSize"
      :background="true"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pagination.total">
    </el-pagination>

<!--    指派维修人员-->
    <el-dialog title="审核" :visible.sync="checkdialogVisible" width="30%">
      <div>车牌号：{{orderInfo.car_no}}</div>
      <br/>
      <div>问题描述：{{orderInfo.question_desc}}</div>
      <br/>
      <div>租借单位：{{orderInfo.rent_unit_name}}</div>
      <br/>
      <el-form  ref="form" :model="form" label-width="100px">
        <el-form-item label="车场：" :label-width="'70px'">
          <el-select v-model="form.fix_factory" >
            <el-option label="玉玺维修厂" value="玉玺维修厂"></el-option>
            <el-option label="金灿维修厂" value="金灿维修厂"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="维修人：" :label-width="'70px'">
          <el-select v-model="form.fix_people" >
            <el-option label="订单中默认" value="订单中默认"></el-option>
            <el-option label="张强" value="张强"></el-option>
            <el-option label="孙一凯" value="孙一凯"></el-option>
            <el-option label="杨晓" value="杨晓"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="checkdialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit">确 定</el-button>
      </span>
    </el-dialog>

    <!--    结束订单-->
    <el-dialog title="结束订单" :visible.sync="enddialogVisible" width="50%">
      <div>车牌号：{{orderInfo.car_no}}</div>
      <br/>
      <div>问题描述：{{orderInfo.question_desc}}</div>
      <br/>
      <div>租借单位：{{orderInfo.rent_unit_name}}</div>
      <br/>
      <div>维修人员：{{orderInfo.fix_people}}</div>
      <br/>
      <el-upload
        :action="host+'/guard/upload2'"
        list-type="picture-card"
        :on-success="handleAvatarSuccess"
        :on-preview="handlePictureCardPreview"
        :on-remove="handleRemove">
        <i class="el-icon-plus"></i>
      </el-upload>
      <el-dialog :visible.sync="dialogVisible">
        <img width="100%" :src="dialogImageUrl" alt="">
      </el-dialog>

      <span slot="footer" class="dialog-footer">
        <el-button @click="enddialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onSubmitPic">确 定</el-button>
      </span>
    </el-dialog>
  </div>



</template>

<script>

import { myGet, myPost,getDateStr } from '@/utils/myUtils'

const urlAdvertLst = '/guard/fixcarorder/lst'
const urlAdvertAdd = '/guard/fixcarorder/add'
const urlAdvertDel = '/guard/fixcarorder/del'
const urlAdvertDetail = '/guard/fixcarorder/detail'
const urlFreeCarLst = '/guard/order/getFreeCarLst'
const urlAllCarLst = '/guard/order/getAllCarLst'
const urlUnitNameLst = '/guard/rentunit/lst?all=1'

export default {

    data() {
      return {
        selectId:[],
        loading:false,
        host:process.env.VUE_APP_HOST,
        list:[],
        isAdd:true,
        checkdialogVisible:false,
        enddialogVisible:false,
        listLoading:false,
        freeCarLst:[],
        allCarLst:[],
        unitList:[],
        form:{
          order_id:0,
          fix_factory:'',
          fix_people:''
        },
        orderInfo:{},
        query:{
          car_no:'',
          rent_unit_id:'',
          status:0,
          user_name:'',
          phone:''
        },
        pagination:{
          page:1,
          pageSize:20,
          total:0,
        },
        fileList:[],
        upload:{
          module:'advert',
        },
        dialogImageUrl: '',
        dialogVisible: false
      }
    },
    methods: {
      handleAvatarSuccess(res, file) {
        console.log(res)
        this.fileList.push(res.filePath)
      },
      handleRemove(file, fileList) {
        console.log(file, fileList);
        this.fileList = []
        fileList.forEach(item=>{
          this.fileList.push(item.response.filePath)
        })

      },
      handlePictureCardPreview(file) {
        this.dialogImageUrl = file.url;
        this.dialogVisible = true;
      },
      showEnd(index,row){
        this.enddialogVisible = true;
        this.orderInfo.fix_people = row.fix_people
        this.orderInfo.car_no = row.car_no
        this.orderInfo.question_desc = row.question_desc
        this.orderInfo.rent_unit_name = row.rent_unit_name
        this.form.order_id = row.id
      },

      showCheck(index,row){
        this.checkdialogVisible = true;
        this.orderInfo.car_no = row.car_no
        this.orderInfo.question_desc = row.question_desc
        this.orderInfo.rent_unit_name = row.rent_unit_name
        this.form.order_id = row.id
      },

      onSubmit() {
        this.checkdialogVisible = true;
        myPost(urlAdvertAdd,this.form).then(res=>{
          if(res.status){
            this.checkdialogVisible = false;
            this.$message.success(res.msg);
            this.getRecords()
          }
        })
      },
      onSubmitPic() {
        this.enddialogVisible = true;
        myPost(urlAdvertAdd,{order_id:this.form.order_id,fix_imgs:this.fileList}).then(res=>{
          if(res.status){
            this.enddialogVisible = false;
            this.$message.success(res.msg);
            this.getRecords()
          }
        })
      },

      getAllUnitName(){
        myGet(urlUnitNameLst).then(res=>{
          if(res.status){
            this.unitList = []
            res.data.records.forEach(unit=>{
              this.unitList.push({
                'id':unit.id,
                'unit_name':unit.unit_name
              })
            })
          }
        })
      },
      getFreeCar(){
        myGet(urlFreeCarLst).then(res=>{
          this.freeCarLst = []
          let cars = res.data.records.forEach(car=>{
            this.freeCarLst.push({
              'id':car.id,
              'car_no':car.car_no,
            })
          })
        })
      },
      getAllCar(){
        myGet(urlAllCarLst).then(res=>{
          this.allCarLst = []
          let cars = res.data.records.forEach(car=>{
            this.allCarLst.push({
              'id':car.id,
              'car_no':car.car_no,
            })
          })
        })
      },


      handleSelectionChange(val) {
        this.selectId = [];
        val.forEach(item=>{
          this.selectId.push(item.id)
        })
      },
      toggleSelection(rows) {
        if (rows) {
          rows.forEach(row => {
            this.$refs.multipleTable.toggleRowSelection(row);
          });
        } else {
          this.$refs.multipleTable.clearSelection();
        }
      },
      handleSizeChange(val) {
        this.pagination.pageSize = val
        this.pagination.page = 1
        this.getRecords()
      },
      handleCurrentChange(val) {
        this.pagination.page  = val
        this.getRecords();
      },

      reload(){
        Object.assign(this.$data.query,this.$options.data().query)
        Object.assign(this.$data.pagination,this.$options.data().pagination)
        this.getRecords()
      },

      queryRecords(){
        Object.assign(this.$data.pagination,this.$options.data().pagination)
        this.getRecords()
      },

      getRecords(){
        this.listLoading = true
        const params = Object.assign(this.query,this.pagination)
        myGet(urlAdvertLst,params).then(res=>{
          this.listLoading = false
          if(res.status){
            this.list = res.data.records
            this.pagination.total = res.data.total
          }
        })
      },

      handleDelete(index,row){
        this.$confirm('确定要取消吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let id = row.id;
          myGet(urlAdvertDel,{id}).then(res=>{
            if(res.status){
              this.$message.success(res.msg)
              this.getRecords()
            }
          })
        });
      },
    },

    mounted() {
      this.getRecords()
      this.getAllCar()
      this.getAllUnitName()
    },
}
</script>

<style>

.upLoadShow .el-upload {
  width: 10rem !important;
  height: 10rem !important;
  line-height: 10rem !important;
}

/*当upLoadHide为true时，启用如下样式，即缩略图的样式，若为false则不启用该样式*/
.upLoadHide .el-upload-list--picture-card .el-upload-list__item {
  width: 10rem !important;
  height: 10rem !important;
  line-height: 10rem !important;
}
/*当upLoadHide为true时，启用如下样式，即上传框的样式，若为false则不启用该样式*/
.upLoadHide .el-upload {
  display: none;
}


.table-img{
    width: 50px;
    height: 50px;
}

.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
